//
//  CEScene.cpp
//  Prototype Template
//
//  Created by Alexander Trutchenko on 01.05.2012
//  Copyright LID 2012. All rights reserved.
//

#include "cocos2dx_extentions/utility/CEScene.h"

#include "cocos2dx_extentions/utility/CEResourceManager.h"
#include "cocos2dx_extentions/animations/CEAnimationFactory.h"
#include "cocos2dx_extentions/utility/CESceneManager.h"
#include "cocos2dx_extentions/localization/CELocale.h"


using namespace cocos2d;

namespace cocosext {

////////////////////////////////////////////////////////
//
/** CEScene
 * 
*/
////////////////////////////////////////////////////////

CEScene* CEScene::scene()
{
	CEScene *pRet = new CEScene();
	CCAssert(pRet != NULL, NULL);
	if (pRet->init())
	{
		pRet->autorelease();
        return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet)
		return NULL;
	}
}

////////////////////////////////////////////////////////
// Constructor, destructor and init

bool CEScene::init()
{
    //self init
    if (!CCScene::init() )
        return false;


    return true;
}

////////////////////////////////////////////////////////
// Main interface
void CEScene::fillScene()
{
	_isFilled = true;
}

void CEScene::clearScene()
{
	_isFilled = false;
	this->stopAllActions();
	this->removeAllChildrenWithCleanup(true);
}

//Cocos2d functions
void CEScene::onEnter()
{
	CCScene::onEnter();

	if (_mustResetTrigger)
	{
		this->setIsVisible(false);
		CESceneManager::sharedSceneManager()->resetCurrentScene(false);
		return;
	}
	
	if (!_isFilled)
	{
		CEAnimationFactory::sharedAnimationFactory()->onAddScene(this);
		CELocale::sharedLocale()->onAddScene(this);
		CEResourceManager::sharedResourceManager()->onAddScene(this);
	}

	CEAnimationFactory::sharedAnimationFactory()->setCurrentScene(this);
	CELocale::sharedLocale()->setCurrentScene(this);
	CEResourceManager::sharedResourceManager()->setCurrentScene(this);

	if (!_isFilled)
		fillScene();	
}

void CEScene::onExit()
{
	CCScene::onExit();

}

void CEScene::cleanup()
{
	CCScene::cleanup();
	
	clearScene();

	CEAnimationFactory::sharedAnimationFactory()->onRemoveScene(this);
	CELocale::sharedLocale()->onRemoveScene(this);
	CEResourceManager::sharedResourceManager()->onRemoveScene(this);
}

} // namespace cocosext
